#include "MsgCenter.h"
#include <assert.h>

DMsgCenter *DMsgCenter::m_pMsgCenter;
QMutex DMsgCenter::m_Mutex;

DMsgCenter *DMsgCenter::getInstance()
{
	if (!m_pMsgCenter)
	{
		m_Mutex.lock();
		if (!m_pMsgCenter)
		{
			m_pMsgCenter = new DMsgCenter();
		}
		m_Mutex.unlock();
	}

	return m_pMsgCenter;
}

void DMsgCenter::destoryInstance()
{
	if (m_pMsgCenter)
	{
		m_Mutex.lock();
		if (m_pMsgCenter)
		{
			delete m_pMsgCenter;
		}
		m_Mutex.unlock();
	}
}

DMsgCenter::DMsgCenter()
{
}


DMsgCenter::~DMsgCenter()
{
}

void DMsgCenter::registMsg(const InnerMsg msg, MsgCallBack &callback)
{
	m_msgMap[msg] = callback;
}

bool DMsgCenter::handleMsg(const InnerMsg msg, const void *data)
{
	auto findRet= m_msgMap.find(msg);
	if (findRet!=m_msgMap.end())
	{
		findRet->second(data);
	}
	else{
		//assert(false);
	}

	return true;
}